﻿using Sys.Core;
using Sys.Core.Models.Dto;
using Sys.Core.Models.Queries;
using Sys.Core.Models.VO;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Infrastructure;
using MiniFox.Platform.Applications.Web;
using MiniFox.Resources;
using MiniFox.Utility;
using MiniFox.Web.Filters;
using MiniFox.Web.Infrastructure;
using System.ComponentModel.DataAnnotations;

namespace Sys.Controllers
{
    [NoCache, ValidationFilter, Area(GlobalConstants.AREA_NAME)]
    public class AppController : AuthorizedController<IAppService>
    {
        [HttpPost]
        public Response<ResultObject<IEnumerable<ApplicationVo>>> Query([FromBody] PagedQueryObject<AppQuery> request)
        {
            var result = this.Component.Query(request);
            return result.CreateResponse();
        }

        [HttpGet]
        public Response<ApplicationVo> Item([FromQuery] string id)
        {
            ApplicationVo app = this.Component.Item(id);
            return app.CreateResponse();
        }

        [HttpGet]
        public Response Toggle([FromQuery] string id)
        {
            this.Component.Toggle(id);
            return new();
        }

        [HttpPost]
        public Response<ApplicationVo> Create([FromBody] ApplicationDto app)
        {
            ApplicationVo result = this.Component.Create(app);
            Response<ApplicationVo> response = result.CreateResponse();

            return response;
        }

        [HttpPost]
        public Response<ApplicationVo> Update([FromBody] ApplicationDto app)
        {
            ApplicationVo result = this.Component.Update(app);
            Response<ApplicationVo> response = result.CreateResponse();

            return response;
        }

        [HttpPost]
        public Response Delete([FromBody] string appId)
        {
            this.Component.Delete(appId);

            return new();
        }

        [HttpGet]
        public Response<string> GetSecret([FromQuery] string id)
        {
            string key = this.Component.GetSecretKey(id);
            return key.CreateResponse();
        }

        [HttpGet]
        public Response NewSecret([FromQuery, Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string id)
        {
            string key = this.Component.NewSecretKey(id);
            return key.CreateResponse();
        }

        [HttpPost]
        public Response<ResultObject<IEnumerable<ModuleVo>>> QueryModules([FromBody] PagedQueryObject<ModuleQuery> request)
        {
            var result = this.Component.QueryModules(request);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response<ModuleVo> CreateModule([FromBody] ModuleDto module)
        {
            var result = this.Component.CreateModule(module);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response<ModuleVo> UpdateModule([FromBody] ModuleDto module)
        {
            var result = this.Component.UpdateModule(module);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response DeleteModule([FromBody] string data)
        {
            this.Component.DeleteModule(data);
            return new();
        }

        [HttpGet]
        public Response<IEnumerable<ModuleActionVo>> ModuleActions([FromQuery] string moduleId)
        {
            var result = this.Component.GetModuleActions(moduleId);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response<ModuleActionVo> CreateModuleAction([FromBody] ModuleActionDto moduleActionDto)
        {
            var result = this.Component.CreateModuleAction(moduleActionDto);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response<ModuleActionVo> UpdateModuleAction([FromBody] ModuleActionDto moduleActionDto)
        {
            var result = this.Component.UpdateModuleAction(moduleActionDto);
            return result.CreateResponse();
        }

        [HttpPost]
        public Response DeleteModuleAction([FromBody] string data)
        {
            this.Component.DeleteModuleAction(data);
            return new();
        }
    }
}
